package com.wang.collectionUtil;

import java.util.*;

/**
 * Collection工具类的使用
 */
public class CollectionUtilDemo {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(20);
        arrayList.add(5);
        arrayList.add(10);
        arrayList.add(30);
        arrayList.add(6);
        //sort
        System.out.println("排序前");
        System.out.println(arrayList.toString());
        Collections.sort(arrayList);
        System.out.println("排序后");
        System.out.println(arrayList.toString());
        //binarySearch二分查找的前提是先排序
        int i = Collections.binarySearch(arrayList, 30);//返回的是下标
        System.out.println(i);
        //copy复制
        ArrayList<Integer> arrayListCopy = new ArrayList<>();
        //这个copy方法有瑕疵.必须要求两个数组一样大的前提才能copy
        for (Integer integer : arrayList) {
            arrayListCopy.add(0);
        }
        Collections.copy(arrayListCopy,arrayList);
        System.out.println(arrayListCopy.toString());
        //翻转reverse
        Collections.reverse(arrayList);
        System.out.println(arrayList);
        //shuffle打乱顺序
        Collections.shuffle(arrayList);
        System.out.println(arrayList);
        //补充
        //1.list集合转为数组
        Integer[] integers = arrayList.toArray(new Integer[0]);//给定长度小于list那么按list的size为准
        System.out.println(integers.length);
        System.out.println(Arrays.toString(integers));
        //2.数组转为集合(Arrays工具包)  记得基本类型不支持(int不行Integer才行!)
        // 但是这个集合是一个受限集合,不能add和remove,
        // 因为数组长度是固定的,转过来list还是不能添加
        List<Integer> integers1 = Arrays.asList(integers);
        System.out.println(integers.toString());
    }
}
